home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000…tember: Reference Library / Dev.CD Sep 00 RL Disk 1.toast / mac / Technical Documentation / Develop / develop Issue 27 / develop Issue 27 code / Internet Config Assistant / toolkit / TBitmap.cp < prev    next >
Encoding:
Text File  |  1996-06-30  |  5.0 KB  |  256 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        TBitmap.cp
  3.  
  4.     Contains:    A simple bitmap class
  5.  
  6.     Written by:    Arno Gourdol
  7.  
  8.     Copyright:    © 1994-1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.  
  11. */
  12.  
  13. #include "TBitmap.h"
  14.  
  15. #include <Resources.h>
  16.  
  17.  
  18.  
  19. // --------------------------------------------------------------------
  20. // TBitmap
  21. // --------------------------------------------------------------------
  22. // Constructor, from a PICT resource, builds a bitmap of the 
  23. // specified depth
  24.  
  25. TBitmap::TBitmap(SInt16 pictureID, UInt16 depth) :
  26.     fGWorld(NULL)
  27. {
  28.     PicHandle picture = (PicHandle)::Get1Resource('PICT', pictureID);
  29.     
  30.     if (picture != NULL)
  31.     {
  32.         // Geet the size of the picturee
  33.         CRect frame((**picture).picFrame);
  34.  
  35.         fBounds = frame;
  36.         
  37.         // Create a new offscreen world of the specified dimensions
  38.         OSErr err = ::NewGWorld(&fGWorld, depth, frame, NULL, NULL, 0);
  39.         assert(err == memFullErr || err == noErr);
  40.         
  41.         if (fGWorld != NULL)
  42.         {
  43.             fPort = (GrafPtr)fGWorld;
  44.             fDepth = depth;
  45.             fIsColor = (fDepth > 1);
  46.         }
  47.         else
  48.         {
  49.             fPort = NULL;
  50.             fDepth = 1;
  51.             fIsColor = false;
  52.         }
  53.         
  54.         // If the offscreen was created successfully, 
  55.         // draw the picture inside it
  56.         if (Lock())
  57.         {
  58.             ::DrawPicture(picture, frame);
  59.             Unlock();
  60.         }
  61.         
  62.         
  63.         ::ReleaseResource((Handle)picture);
  64.     }
  65. }
  66.  
  67.  
  68.  
  69. // --------------------------------------------------------------------
  70. // TBitmap
  71. // --------------------------------------------------------------------
  72. // Constructor. Creates a bitmap of the specified dimensions and depth
  73.  
  74. TBitmap::TBitmap(CRect bounds, UInt16 depth) :
  75.     fBounds(bounds),
  76.     fDepth(depth),
  77.     fGWorld(NULL)
  78. {
  79.     fGWorld = NULL;
  80.     
  81.     CRect rect(fBounds);
  82.     // If the requested depth is 0...
  83.     if (depth == 0)
  84.     {
  85.         // ... convert the dimension of the bitmap to screen 
  86.         // coordinates. This will cause NewGWorld to use the deepest
  87.         // depth available on this area of the screen
  88.         TDrawContext drawContext;
  89.         drawContext.ConvertToScreen(rect);
  90.     }
  91.     
  92.     OSErr err = ::NewGWorld(&fGWorld, depth, rect, NULL, NULL, 0);
  93.     assert(err == memFullErr || err == noErr);
  94.  
  95.     if (fGWorld != NULL)
  96.     {
  97.         fPort = (GrafPtr)fGWorld;
  98.         fDepth = depth;
  99.         fIsColor = (fDepth > 1);
  100.     }
  101.     else
  102.     {
  103.         fPort = NULL;
  104.         fDepth = 1;
  105.         fIsColor = false;
  106.     }
  107. }
  108.  
  109.  
  110.  
  111. // --------------------------------------------------------------------
  112. // ~TBitmap
  113. // --------------------------------------------------------------------
  114.  
  115. TBitmap::~TBitmap()
  116. {
  117.     if (fGWorld != NULL)
  118.     {
  119.         ::DisposeGWorld(fGWorld);
  120.     }
  121. }
  122.  
  123.  
  124.  
  125. // --------------------------------------------------------------------
  126. // SetBits
  127. // --------------------------------------------------------------------
  128.  
  129. void TBitmap::SetBits(const void* data, UInt32 length, UInt32 offset, UInt16 depth)
  130. {
  131. #pragma unused(data, length, offset, depth)
  132.  
  133. }
  134.  
  135.  
  136.  
  137. // --------------------------------------------------------------------
  138. // Bits
  139. // --------------------------------------------------------------------
  140.  
  141. void* TBitmap::Bits(void) const
  142. {
  143.     assert(fLockCount > 0);
  144.     
  145.     if (fGWorld != NULL)
  146.     {
  147.         return ::GetPixBaseAddr(::GetGWorldPixMap(fGWorld));
  148.     }
  149.     else
  150.     {
  151.         return NULL;
  152.     }
  153. }
  154.  
  155.  
  156.  
  157. // --------------------------------------------------------------------
  158. // BitsLength
  159. // --------------------------------------------------------------------
  160.  
  161. long TBitmap::BitsLength(void) const
  162. {
  163.     return 0;
  164. }
  165.  
  166.  
  167.  
  168. // --------------------------------------------------------------------
  169. // BytesPerRow
  170. // --------------------------------------------------------------------
  171.  
  172. long TBitmap::BytesPerRow(void) const
  173. {
  174.     if (fGWorld != NULL)
  175.     {
  176.         return (**GetGWorldPixMap(fGWorld)).rowBytes;
  177.     }
  178.     else
  179.     {
  180.         return 0;
  181.     }
  182. }
  183.  
  184.  
  185.  
  186. // --------------------------------------------------------------------
  187. // ColorSpace
  188. // --------------------------------------------------------------------
  189.  
  190. UInt16 TBitmap::ColorSpace(void) const
  191. {
  192.     return fDepth;
  193. }
  194.  
  195.  
  196.  
  197. // --------------------------------------------------------------------
  198. // Bounds
  199. // --------------------------------------------------------------------
  200.  
  201. CRect TBitmap::Bounds(void) const
  202. {
  203.     return fBounds;
  204. }
  205.  
  206.  
  207.  
  208. // --------------------------------------------------------------------
  209. // Lock
  210. // --------------------------------------------------------------------
  211. // Prepare the Bitmap for drawing. Retruns true if the bitmap is 
  212. // ready and drawing can proceed
  213.  
  214. Boolean TBitmap::Lock(void)
  215. {
  216.     Boolean result = false;
  217.     
  218.     if (fGWorld != NULL && ::LockPixels(::GetGWorldPixMap(fGWorld)))
  219.     {
  220.         ::GetGWorld((CGrafPtr*)&fSavePort, &fSaveGDevice);
  221.         result = TDrawContext::Lock();
  222.         if (result)
  223.         {
  224.             ::SetGWorld(fGWorld, NULL);
  225.         }
  226.         else
  227.         {
  228.             ::UnlockPixels(::GetGWorldPixMap(fGWorld));
  229.         }
  230.     }
  231.  
  232.     return result;
  233. }
  234.  
  235.  
  236.  
  237. // --------------------------------------------------------------------
  238. // Unlock
  239. // --------------------------------------------------------------------
  240. // Call when the drawing in the bitmap is done. Only call if Lock() 
  241. // returned true.
  242. // Restores the drawing environmnt
  243.  
  244. void TBitmap::Unlock(void)
  245. {
  246.     assert(fGWorld != NULL);
  247.     
  248.     ::UnlockPixels(::GetGWorldPixMap(fGWorld));
  249.     ::SetGWorld((CGrafPtr)fSavePort, fSaveGDevice);
  250.     
  251.     TDrawContext::Unlock();
  252. }
  253.  
  254.  
  255.  
  256.